import { addon, Permission } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import { AppServiceUtility } from "src/projects/app/appService";
import { message, Select } from "antd";
import { MainContent } from "src/business/components/style-components/main-content";
import FormCreator, { InputType } from "src/business/components/buss-components/form-creator";
import { edit_props_info } from "src/projects/app/util-tool";
import { GetFieldDecoratorOptions } from "antd/lib/form/Form";
import { InputType as ListInputType } from "src/business/components/buss-components/sign-frame-layout";
import { ROUTE_PATH } from "src/projects/router";
let { Option } = Select;
message.config({
    top: 350,
});

/**
 * 组件：帮长者预定购买服务状态
 */
export interface ReserveBuyServiceEditViewState extends ReactViewState {

    /** 滚动条 */
    loading?: boolean;
    /** 基础数据 */
    base_data: any;
    /** 服务商列表 */
    service_p_list?: any[];
    /** 服务列表 */
    service?: any[];
    /** 价格 */
    total_price?: number;
    /** 人员类别 */
    personnel_classification_list?: any;
}

/**
 * 组件：帮长者预定购买服务视图
 */
export class ReserveBuyServiceEditView extends ReactView<ReserveBuyServiceEditViewControl, ReserveBuyServiceEditViewState> {
    private columns_data_source = [{
        title: '编号',
        dataIndex: 'code',
        key: 'code',
    }, {
        title: '姓名',
        dataIndex: 'name',
        key: 'name',
    }, {
        title: '身份证号',
        dataIndex: 'id_card',
        key: 'id_card',
    }, {
        title: '性别',
        dataIndex: 'sex',
        key: 'sex',
    }, {
        title: '出生年月',
        dataIndex: 'birth_date',
        key: 'birth_date',
    }];
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            base_data: {},
            service_p_list: [],
            service: [],
            total_price: 0,
            personnel_classification_list: [],
        };
    }
    /** 确定回调 */
    handleSubmit = (err: Error, values: any) => {
        // let id = this.props.match!.params.key;
        const isTrue: boolean = (!values.personnel_classification && !values.name) || (values.personnel_classification && values.name);
        if (isTrue) {
            message.info('只能选择长者类型或长者其中一个');
            return;
        }
        if (values) {
            let key = this.props.match!.params.key;
            AppServiceUtility.comfirm_order_service.update_reserve_service!({
                'id': key,
                'personnel_classification': values.personnel_classification,
                'service_product_id': values.service_product_id,
                'name': values.name,
                // 'count': values.count,
                'service_provider_id': values.service_provider_id,
                'comment': values.comment,
                'organization_id': values.service_provider_id,
                'count': values.count,
                'buy_type': '补贴账户',
            })!
                .then((data: any) => {
                    if (data === 'Success') {
                        message.info('预约成功');
                        this.props.history!.push(ROUTE_PATH.reserveBuyService);
                    } else {
                        message.error(data);
                    }
                })
                .catch(error => {
                    message.error(error.message);
                });
        }
    }
    componentDidMount() {
        let key = this.props.match!.params.key;
        if (key) {
            AppServiceUtility.comfirm_order_service.get_reserve_service!({ 'id': key })!
                .then(data => {
                    AppServiceUtility.service_item_service.get_service_product_package_list!({ "org_id": data.result![0].provider_id }, 1, 999)!
                        .then(data => {
                            this.setState({
                                service: data!.result
                            });
                        })
                        .catch(err => {
                            console.info(err);
                        });
                    this.setState({
                        base_data: data.result![0],
                        total_price: data.result![0].count
                    });
                })
                .catch(err => {
                    console.info(err);
                });
        }
        AppServiceUtility.person_org_manage_service.get_organization_list!({ "personnel_category": "服务商" })!
            .then(data => {
                this.setState({
                    service_p_list: data!.result
                });
            })
            .catch(err => {
                console.info(err);
            });
        // 人员类别列表
        AppServiceUtility.person_org_manage_service.get_personnel_classification_list!({})!
            .then(data => {
                this.setState({
                    personnel_classification_list: data!.result
                });
            })
            .catch(err => {
                console.info(err);
            });
    }
    service_providerChange = (value: any) => {
        AppServiceUtility.service_item_service.get_service_product_package_list!({ "org_id": value }, 1, 999)!
            .then(data => {
                this.setState({
                    service: data!.result
                });
            })
            .catch(err => {
                console.info(err);
            });
    }
    service_change = (value: any) => {
        AppServiceUtility.service_item_service.get_service_product_package_list!({ "id": value }, 1, 999)!
            .then(data => {
                this.setState({
                    total_price: data!.result![0]['total_price'],
                });
            })
            .catch(err => {
                console.info(err);
            });
    }
    render() {
        let { service_p_list, service } = this.state;
        let org_type_list: any[] = [];
        let service_list: any[] = [];
        service_p_list!.map((item) => {
            org_type_list.push(<Option key={item.id}>{item.name}</Option>);
        });
        service!.map((item) => {
            service_list.push(<Option key={item.id}>{item.name}</Option>);
        });
        let personnel_classification_list: JSX.Element[] = this.state.personnel_classification_list.map((value: any) => {
            return (
                <Option key={value.id}>{value.name}</Option>
            );
        });
        let modal_search_items_props = {
            type: 'input',
            edit_form_items_props: [
                {
                    type: ListInputType.input,
                    label: "长者名称",
                    decorator_id: "name"
                },
                {
                    type: ListInputType.input,
                    label: "身份证号码",
                    decorator_id: "id_card"
                },
            ],
            columns_data_source: this.columns_data_source,
            service_name: AppServiceUtility.person_org_manage_service,
            service_func: 'get_personnel_elder',
            title: '长者查询',
            select_option: {
                placeholder: "请选择长者",
            }
        };
        let service_option = {
            service_object: AppServiceUtility.person_org_manage_service,
            operation_option: {
                save: {
                    func_name: "update_organization"
                },
                query: {
                    func_name: "",
                    arguments: [{ id: this.props.match!.params.key }, 1, 1]
                }
            },
        };
        let base_date = this.state.base_data;
        let edit_props = {
            form_items_props: [
                {
                    title: "预定服务购买",
                    need_card: true,
                    input_props: [
                        {
                            type: InputType.modal_search,
                            label: "长者姓名",
                            decorator_id: 'name',
                            field_decorator_option: {
                                rules: [{ message: "请输入名称" }],
                                initialValue: base_date.name,
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请选择长者姓名",
                                modal_search_items_props: modal_search_items_props
                            },
                        },
                        {
                            type: InputType.select,
                            label: "长者类型",
                            decorator_id: "personnel_classification",
                            field_decorator_option: {
                                rules: [{ message: "请选择长者类型" }],
                            } as GetFieldDecoratorOptions,
                            option: {
                                childrens: personnel_classification_list,
                                placeholder: "请选择长者类型",
                            },
                        },
                        {
                            type: InputType.select,
                            label: "服务商",
                            decorator_id: "service_provider_id",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入服务商" }],
                                initialValue: base_date.provider_id,
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请选择服务商",
                                childrens: org_type_list,
                                onChange: this.service_providerChange

                            }
                        }, {
                            type: InputType.select,
                            label: "服务产品",
                            decorator_id: "service_product_id",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请选择服务产品" }],
                                initialValue: base_date.product_id,
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请选择服务产品",
                                childrens: service_list,
                                onChange: this.service_change
                            }
                        },
                        {
                            type: InputType.antd_input,
                            label: "金额",
                            decorator_id: "count",
                            field_decorator_option: {
                                rules: [{ required: false, message: "" }],
                                initialValue: this.state.total_price,
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "金额",
                            }
                        }, {
                            type: InputType.text_area,
                            label: "备注",
                            decorator_id: "comment",
                            field_decorator_option: {
                                initialValue: base_date.comment,
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入备注",
                                row: 3,
                            }
                        },
                    ]
                }
            ],
            other_btn_propps: [
            ],
            submit_btn_propps: {
                text: "确定",
                cb: this.handleSubmit
            },
            service_option: service_option,
            id: this.props.id,
        };
        // let succ_func = () => { this.props.history!.push(ROUTE_PATH.organization); }
        let edit_props_list = Object.assign(edit_props, edit_props_info);
        return (
            (
                <MainContent>
                    <FormCreator {...edit_props_list} />
                </MainContent>
            )
        );
    }
}

/**
 * 控件：帮长者预定购买服务编辑控件
 * @description 帮长者预定购买服务编辑控件
 * @author
 */
@addon('ReserveBuyServiceEditView', '帮长者预定购买服务编辑控件', '帮长者预定购买服务编辑控件')
@reactControl(ReserveBuyServiceEditView, true)
export class ReserveBuyServiceEditViewControl extends ReactViewControl {
    /** 视图权限 */
    public permission?: Permission;

}